/**
 * poll
 *
 +-------------------------------------------------
 * @link
 * @copyright
 * @author Haijun Wu <nicholasinlove@126.com>
 +------------------------------------------------- 
 * 
 * $Id: poll.js 67 2011-03-26 13:06:51Z nicholasinlove1986@gmail.com $
 */

(function() {

	function polls() {
		var frm     = document.forms['polls_form'];
		var type    = frm.elements['type'].value;
		var view    = frm.elements['view'].value;
		var id      = frm.elements['poll_id'].value;
		var limit   = frm.elements['limit'].value;
		var option_id = 0;
		var layer = $("#polls-ans");
		$polls_submit = $('#polls_submit');
		$polls_submit.attr('disabled', false);
		
		if (frm.elements['option_id'].checked) {
			option_id = frm.elements['option_id'].value;
		} else {
			for (i=0; i<frm.elements['option_id'].length; i++ ) {
				if (frm.elements['option_id'][i].checked) {
					option_id = (type == 1) ? option_id + "," + frm.elements['option_id'][i].value : frm.elements['option_id'][i].value;
				}
			}
		}
		
		if (option_id == 0) {
			return;
		} else {
			jQuery.ajax({
				type:        'post',
				url:          APP+'/Vote/vote',
				dataType:    'json',
				data:        'ajax=1&act=submit&id='+id + '&options_id=' + option_id + '&type=' + type + '&limit=' + limit + '&view=' + view, 
				beforeSend: function(json) {
					document.body.style.cursor = 'wait';
					jQuery('.wp-polls-loading').html('<img src="'+PUBLIC+'/Images/ajax-loader.gif" border="0" alt="loading..." align="absmiddle"> Loading...').slideDown();
					$polls_submit.attr('disabled', true).fadeTo('slow', 0);
				},
				success: function(json) {
					if (json.status==1) {
						jQuery('.wp-polls-loading').fadeOut();
						if (layer.length > 0){
							layer.css("display","none");
							layer.html(json.data).fadeIn('slow');
						}
					} else {
						jQuery('.wp-polls-loading').html(json.info).fadeIn('slow');
						window.setTimeout(function () {
							jQuery('.wp-polls-loading').html(json.info).slideUp();
							$polls_submit.attr('disabled', false).fadeTo('slow', 1);
						}, 3000);
					}
						document.body.style.cursor = 'auto';
				},
				error: function(json) {
					jQuery('.wp-polls-loading').html('Oops, failed to load data.').fadeIn('slow');
				}				
			});
		}  
	}
		
	function checkbox(obj) {
		var p = 0;
		if(obj.checked) {
			p++;
		} else {
			p--;
		}
		document.getElementById("polls_submit").disabled = p > 0 ? false : true;
	}
	
	function checkradio(obj){
		if(obj.checked){
			document.getElementById("polls_submit").disabled = false;
		}
	}

	window['NICJS'] = {};
	window['NICJS']['checkbox'] = checkbox;
	window['NICJS']['checkradio'] = checkradio;
	window['NICJS']['polls'] = polls;

})();
